#define _CRT_SECURE_NO_WARNINGS 1
class Solution {
public:
    vector<vector<int>> threeSum(vector<int>& nums) {
        vector<vector<int>>vv;
        int  n = nums.size();
        sort(nums.begin(), nums.end());
        int begin = 0;
        while (begin < n - 2)
        {
            int left = begin + 1, right = n - 1;
            while (left < right)
            {
                if (nums[left] + nums[right] > -nums[begin])
                {
                    right--;
                }
                else if (nums[left] + nums[right] < -nums[begin])
                {
                    left++;
                }
                else
                {
                    vv.push_back({ nums[begin],nums[left],nums[right] });
                    left++, right--;
                    while (left < right && nums[left] == nums[left - 1])
                    {
                        left++;
                    }
                    while (left < right && nums[right] == nums[right + 1])
                    {
                        right--;
                    }
                }
            }
            begin++;
            while (begin < n - 2 && nums[begin] == nums[begin - 1]) begin++;
        }
        return vv;
    }
};